import java.awt.EventQueue;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.management.InstanceNotFoundException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import request.ServerRequestKind;

public class Frame extends JFrame implements IExecutorListener {

	private JPanel contentPane;
	private JTextField tfServer;
	private JTextField tfPort;
	private JTextArea taLog = new JTextArea();
	private TrayIcon trayIcon;
	private static Frame frame;

	/**
	 * Launch the application.
	 */
	public static void exec(final Config config, final Executor executor,
			final TrayIcon trayIcon) {
		EventQueue.invokeLater(new Runnable() {

			public void run() {
				try {
					if (frame == null) {
						frame = new Frame(executor, trayIcon);
						frame.tfPort.setText(config.getPort() + "");
						frame.tfServer.setText(config.getServer());
					}
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * 
	 * @param trayIcon
	 */
	public Frame(final Executor executor, TrayIcon trayIcon) {
		this.trayIcon = trayIcon;
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 218, 387);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(null);
		setContentPane(contentPane);

		tfServer = new JTextField();
		tfServer.setBounds(10, 27, 165, 20);
		contentPane.add(tfServer);
		tfServer.setColumns(10);

		JLabel lblPorta = new JLabel("Server");
		lblPorta.setBounds(10, 11, 46, 14);
		contentPane.add(lblPorta);

		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(10, 58, 46, 14);
		contentPane.add(lblPort);

		tfPort = new JTextField();
		tfPort.setColumns(10);
		tfPort.setBounds(10, 74, 86, 20);
		contentPane.add(tfPort);

		JButton btnConnect = new JButton("Connect");

		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					executor.connect();
				} catch (Exception e1) {
					e1.printStackTrace();
				}
			}
		});

		btnConnect.setBounds(10, 126, 89, 23);
		contentPane.add(btnConnect);

		JButton btnRunRemote = new JButton("Run remote");
		btnRunRemote.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					executor.runRemote();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		btnRunRemote.setBounds(100, 126, 89, 23);
		contentPane.add(btnRunRemote);

		taLog.setBounds(10, 179, 179, 159);
		contentPane.add(taLog);

		JLabel lblLog = new JLabel("Log");
		lblLog.setBounds(10, 160, 46, 14);
		contentPane.add(lblLog);
	}

	@Override
	public void addLog(ServerRequestKind request, String msg) {
		switch (request) {
		case RUNNING:
			addTextLog("The server is running.");
			break;

		case ERROR:
			trayIcon.displayMessage("Problems", msg, MessageType.ERROR);
			addTextLog("The server is running.");
			break;
		case WARNING:
		case INFO:
			String[] info = new String[] { "", "Info", "Warning" };
			taLog.setText(taLog.getText() + "\n" + info[request.ordinal()]
					+ msg);
			break;

		default:
			break;
		}
	}

	private void addTextLog(String msg) {
		taLog.setText(taLog.getText() + msg);
	}
}
